<template>
  <div>
    <div
      style="
        margin-bottom: 20px;
        margin: 0 auto;
        margin: 10px;
        padding: 10px;
        background-color: #fff;
        box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px;
      "
    >
      <div>
        <el-input
          style="width: 260px; margin-right: 10px"
          v-model="data.name"
          placeholder="请输入用户名字查询"
          :prefix-icon="Search"
        />
        <el-button type="primary" style="margin-left: 10px" @click="load"
          >查询</el-button
        >
        <el-button type="info" @click="reset">重置</el-button>
        <el-button
          type="primary"
          @click="handleAdd"
          style="background-color: #409eff"
          >新增</el-button
        >
      </div>
    </div>

    <div
      class="card"
      style="margin: 10px; box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px"
    >
      <div>
        <el-table :data="data.tableData" style="width: 100%">
          <el-table-column
            align="center"
            type="index"
            label="序号"
            width="80"
          />
          <el-table-column align="center" prop="userName" label="用户名" />
          <!-- <el-table-column align="center" prop="password" label="密码" /> -->
          <el-table-column align="center" prop="phone" label="手机号码" />
          <el-table-column align="center" prop="createTime" label="创建时间" />
          <el-table-column align="center" prop="updateTime" label="修改时间" />

          <el-table-column #default="scope" width="180">
            <el-button type="primary" plain @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button type="danger" plain @click="del(scope.row.userId)"
              >删除</el-button
            >
          </el-table-column>
        </el-table>

        <div
          style="
            background-color: #fff;
            display: flex;
            justify-content: flex-end;
            padding: 10px;
          "
        >
          <el-pagination
            style="padding: 10px; text-align: right; margin-right: 10px"
            v-model:current-page="data.page"
            v-model:page-size="data.pageSize"
            @current-change="handleCurrentChange"
            background
            layout="prev,pager,next"
            :total="data.total"
          ></el-pagination>
        </div>
      </div>
    </div>

    <el-dialog width="35%" v-model="data.FormVisible" title="用户信息">
      <el-form
        :model="data.form"
        label-width="100px"
        label-position="right"
        style="padding-right: 40px"
      >
        <el-form-item label="用户名">
          <el-input v-model="data.form.userName" autocomplete="off" />
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="data.form.password" autocomplete="off" />
        </el-form-item>
        <el-form-item label="手机号码">
          <el-input v-model="data.form.phone" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="data.FormVisible = false">取 消</el-button>
          <el-button type="primary" @click="save"> 确 认 </el-button>
        </span>
      </template>
    </el-dialog>
    <el-dialog v-model="data.centerDialogVisible" width="500" title="提示">
      <span>是否确定删除用户</span>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="data.centerDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="delUser"> 确定 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
  
  <script setup>
import { reactive } from "vue";
import { Search } from "@element-plus/icons-vue";
import { ElMessage, ElMessageBox } from "element-plus";
import request from "@/utils/http";

const data = reactive({
  userName: "",
  password: "",
  phone: "",
  userId: "",
  createTime: "",
  updateTime: "",
  tableData: [],
  total: 0,
  pageSize: 10, //每页的个数
  page: 1, //当前的页码
  FormVisible: false,
  centerDialogVisible: false,
  form: {},

  //上传后的文件列表
  fileList: [],
  //请求头
  headers: { "Content-Type": "multipart/form-data" },
});

const load = () => {
  request
    .get("/userAPI/user", {
      params: {
        page: data.page,
        pageSize: data.pageSize,
        name: data.name,
      },
    })
    .then((res) => {
      data.tableData = res.data?.records || [];
      data.total = res.data?.total || 0;
    });
};
//调用方法获取后端数据
load();

const handleCurrentChange = (pageNum) => {
  //当翻页的时候 重新加载数据即可
  load();
};

//重置数据
const reset = () => {
  data.name = "";
  load();
};

//点击打开新增表单，并重置数据
const handleAdd = () => {
  data.form = {};
  data.FormVisible = true;
};

//发送新增请求
const save = () => {
  console.log(data.form);
  request
    .request({
      url: data.form.userId ? "/userAPI/user" : "/userAPI/user/register",
      method: data.form.userId ? "PUT" : "POST",
      data: {
        userId: data.form.userId,
        userName: data.form.userName,
        password: data.form.password,
        phone: data.form.phone,
      },
    })
    .then((res) => {
      if (res.code === 1) {
        load(); //重新加载数据
        data.FormVisible = false; //关闭弹窗
        ElMessage.success("操作成功");
      } else {
        ElMessage.error(res.msg);
      }
    });
};

const handleEdit = (row) => {
  data.form = JSON.parse(JSON.stringify(row));
  data.FormVisible = true;
};

const delUser = () => {
  request.delete("/userAPI/user/" + data.userId).then((res) => {
    if (res.code === 1) {
      load(); //重新加载数据
      data.centerDialogVisible = false
      ElMessage.success("删除成功");
    } else {
      ElMessage.error(res.msg);
    }
  });
};

const del = (id) => {
  data.centerDialogVisible = true;
  data.userId = id;
  //   ElMessageBox.confirm("删除数据后无法恢复，您确认删除吗？", "删除确认", {
  //     type: "warning",
  //   })
  //     .then((res) => {
  //       request.delete("/user/" + id).then((res) => {
  //         if (res.code === 1) {
  //           load(); //重新加载数据
  //           ElMessage.success("操作成功");
  //         } else {
  //           ElMessage.error(res.msg);
  //         }
  //       });
  //     })
  //     .catch((res) => {});
};

const outputExcel = () => {
  location.href = "http://localhost:8080/download/user";
};

const uploadFile = (item) => {
  //上传文件的需要formdata类型;所以要转
  let FormDatas = new FormData();
  FormDatas.append("file", item.file);
  request
    .request({
      url: "upload/user",
      method: "post",
      data: FormDatas,
      headers: data.headers,
    })
    .then((res) => {
      if (res.code === 1) {
        load(); //重新加载数据
        ElMessage.success("文件上传成功");
      } else {
        ElMessage.error(res.msg);
      }
    });
};
</script>

<style>
* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

.dialog {
  height: 50%;
}
</style>